package com.infraware.supermanbooks;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Metadata;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.epub.EpubReader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.infraware.supermanbooks.book.BookContent;
import com.infraware.supermanbooks.book.BookContent.BookItem;

public class BookListAdapter extends ArrayAdapter<BookContent.BookItem> {

    private final String TAG = "BookListAdapter";

    private Context context;
    private int layoutResource;
    private int textViewResourceId;
    private List<BookContent.BookItem> bookList;

    public BookListAdapter(Context context, int resource, int textViewResourceId, List<BookContent.BookItem> objects) {
        super(context, resource, textViewResourceId, objects);

        this.context = context;
        this.layoutResource = resource;
        this.textViewResourceId = textViewResourceId;
        this.bookList = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        View layoutToBeReturned = null;

        if (convertView != null) {
            layoutToBeReturned = convertView;
            // Get the ViewHolder back to get fast access to views
            holder = (ViewHolder)convertView.getTag();
        } else {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layoutToBeReturned = inflater.inflate(R.layout.fragment_book_list, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();

            holder.cover = (ImageView)layoutToBeReturned.findViewById(R.id.book_cover);
            holder.title = (TextView)layoutToBeReturned.findViewById(R.id.book_title);
            holder.author = (TextView)layoutToBeReturned.findViewById(R.id.book_author);

            layoutToBeReturned.setTag(holder);
        }

        BookItem item = bookList.get(position);
        if (item.cover != null && item.cover.get() != null) {
            holder.cover.setImageBitmap(item.cover.get());
        } else {
            holder.cover.setImageResource(R.drawable.ic_launcher);
        }

        holder.title.setText(item.title);
        holder.author.setText(item.author);

        return layoutToBeReturned;
    }

    static class ViewHolder {
        ImageView cover;
        TextView title;
        TextView author;
    }

    public void add(String pathname) {

        try {
            File file = new File(pathname);
            FileInputStream in = new FileInputStream(file);
            EpubReader reader = new EpubReader();
            Book book = reader.readEpub(in);
            Metadata metaData = book.getMetadata();

            String id = String.valueOf(file.getName().hashCode());
            String title = book.getTitle();
            String author = "";
            if (metaData.getAuthors().size() > 0) {
                author = metaData.getAuthors().get(0).toString();
            }

            BookItem bookItem = new BookItem(pathname, id, title, author);
            BookContent.addItem(bookItem);

            Resource coverResource = book.getCoverImage();
            if (coverResource != null) {
                Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
                bookItem.cover = coverImage != null ? new SoftReference<Bitmap>(coverImage) : null;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
